#include "pch.h"
#include "AIS_My2dShape.h"
#include "Prs3d_ShadingAspect.hxx"
#include "Graphic3d_ArrayOfTriangles.hxx"

IMPLEMENT_STANDARD_RTTIEXT(AIS_My2dShape, AIS_InteractiveObject)
IMPLEMENT_STANDARD_HANDLE(AIS_My2dShape, AIS_InteractiveObject)

AIS_My2dShape::AIS_My2dShape()
	:myX(0),
	myY(0),
	myCX(100),
	myCY(100)
{
	SetTransformPersistence(new Graphic3d_TransformPers(Graphic3d_TMF_2d, Aspect_TOTP_LEFT_UPPER));
	SetZLayer(Graphic3d_ZLayerId_TopOSD);
}


AIS_My2dShape::AIS_My2dShape(int x, int y, int cx, int cy)
	:myX(x),
	myY(y),
	myCX(cx),
	myCY(cy)
{
	SetTransformPersistence(new Graphic3d_TransformPers(Graphic3d_TMF_2d, Aspect_TOTP_LEFT_UPPER));
}

#include "OpenGl_Group.hxx"
#include "Graphic3d_Text.hxx"
#include "OpenGl_PrimitiveArray.hxx"
void AIS_My2dShape::Compute(const Handle(PrsMgr_PresentationManager3d)&,
	const Handle(Prs3d_Presentation)& thePrs,
	const Standard_Integer)
{
	Handle(Graphic3d_Group) aLabelsGroup = thePrs->NewGroup();

	const Handle(Prs3d_TextAspect)& anAspect = myDrawer->TextAspect();
	anAspect->SetColor(Quantity_NOC_RED);

	Handle(Graphic3d_Text) aText = new Graphic3d_Text(8);
	aText->SetText("CAM3");
	aText->SetOrientation(gp_Ax2(gp_Pnt(0, 0, 0.0), gp::DZ()));
	//	aText->SetOwnAnchorPoint(Standard_False);
	//	aText->SetVerticalAlignment(Graphic3d_VTA_CENTER);

	aLabelsGroup->AddText(aText);

	Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();

	aShadingAspect->SetColor(Quantity_NOC_GRAY);
	aShadingAspect->SetTransparency(0.5, Aspect_TOFM_FRONT_SIDE);
	aShadingAspect->Aspect()->SetAlphaMode(Graphic3d_AlphaMode_Blend);
	aShadingAspect->Aspect()->SetShadingModel(Graphic3d_TOSM_UNLIT);
	Handle(Graphic3d_ArrayOfTriangles) aPrims = new Graphic3d_ArrayOfTriangles(4, 3 * 2,
		Standard_True, Standard_False, Standard_True);

	gp_Dir aNorm(0, 0, -1);
	Standard_Real x1 = myX + myCX;
	Standard_Real y1 = myY + myCY;

	aPrims->AddVertex(gp_Pnt(myX, -myY, 0), aNorm, gp_Pnt2d(0, 0));
	aPrims->AddVertex(gp_Pnt(x1, -myY, 0), aNorm, gp_Pnt2d(1, 0));
	aPrims->AddVertex(gp_Pnt(x1, -y1, 0), aNorm, gp_Pnt2d(1, 1));
	aPrims->AddVertex(gp_Pnt(myX, -y1, 0), aNorm, gp_Pnt2d(0, 1));
	aPrims->AddEdges(1, 2, 3);
	aPrims->AddEdges(1, 3, 4);

	Handle(Graphic3d_Group) theGroup = thePrs->CurrentGroup();
	theGroup->SetClosed(Standard_True);
	theGroup->SetGroupPrimitivesAspect(aShadingAspect->Aspect());
	theGroup->AddPrimitiveArray(aPrims);



	Handle(OpenGl_Group) theGlGroup = Handle(OpenGl_Group)::DownCast(theGroup);
	for (const OpenGl_ElementNode* node = theGlGroup->FirstNode(); node != nullptr; node = node->next) {
		OpenGl_PrimitiveArray* e = (OpenGl_PrimitiveArray*)node->elem;
		TRACE(L"\n[UID]UID=%d", e->GetUID());
	}
}
